<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <button @click="comName='mycom1'">显示mycom1</button>
        <button  @click="comName='mycom2'">显示mycom2</button>

        <!-- commponent是vue中动态组件：它是一个容器，自已是没有内容，你可以在其中放入任意的组件 
        它有一个属性名：is ,值是一个字符串，就是当前注册的组件的名字
        -->

        <component :is="comName"></component>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>
        var vm = new Vue({
            el: '#app',
            components: {
                mycom1: {
                    template: `<div> 子组件MyCom1 {{d}}</div>`,
                    data(){return {d: Date.now()}},
                    created() {
                        console.log('com1:创建')
                    },
                    destroyed() {
                        console.log('com1: destory')
                    }
                },
                mycom2: {
                    template: `<div> 子组件MyCom2</div>`,
                    created() {
                        console.log('com2:创建')
                    },
                    destroyed() {
                        console.log('com2: destory')
                    }
                },
            },
            data: {
                comName: 'mycom1'
            }
        })
    </script>
</body>

</html>